package com.hankol.community

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.baselibrary.Constants
import com.baselibrary.bean.WikiHomeAmusingMoreRespWiki
import com.baselibrary.extension.checkNotNull
import com.baselibrary.manager.LoadingManager
import com.baselibrary.manager.SensorsDataManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ARouterUtils
import com.baselibrary.utils.DoubleUtils
import com.baselibrary.activity.BaseActivity
import com.baselibrary.utils.EmptyViewUtil
import com.hankol.community.adapter.SquareWikiAdapter
import com.hankol.community.bean.ForumClassifyListBean
import com.hankol.community.databinding.ActivityWikiListBinding
import com.hankol.community.view.GridSpaceItemDecoration
import com.hankol.community.viewmodel.CommunityViewModel
import java.util.HashMap


/**
 * @description 百科列表【首页大改版】
 *@author wxp
 */
@Route(path = ActivityRouter.COMMUNITY.WIKI_LIST)
class WikiListActivity : BaseActivity() {

    var page = 1
    private var hasnext: Boolean = false
    private val adapter by lazy { SquareWikiAdapter() }
    private lateinit var binding: ActivityWikiListBinding
    private lateinit var mViewModel: CommunityViewModel

    override fun getLayoutId(): Int {
        return R.layout.activity_wiki_list
    }

    override fun init() {
        binding = getDataBinding<ActivityWikiListBinding>()
        mViewModel = getActivityViewModelProvider(this).get(CommunityViewModel::class.java)

        LoadingManager.showLoadingDialog(this)
        initView()
        initListener()
        initObserver()
        loadData()
        SensorsDataManager.instance.wikiView()
    }

    private fun initView() {
//        binding.rvGoodList.layoutManager = GridLayoutManager(this, 2)
//        binding.rvGoodList.addItemDecoration(GridSpaceItemDecoration(2, 20, 0))
        binding.rvList.adapter = adapter
    }

    private fun initObserver() {
        mViewModel.wikiMld.observe(this, Observer {
            LoadingManager.hideLoadingDialog(this)
            binding.smartRefresh.finishRefresh()
            if (it.size == 0) {
                //无数据
                if (page != 1) {
                    binding.smartRefresh.finishLoadMore()
                }
                adapter.emptyView = EmptyViewUtil.getEmptyView(Constants.TYPE_GOOD_LIST, binding.rvList, "暂无百科")
            } else {
                if (it.checkNotNull()) {
                    hasnext = it.size != adapter.data.size
                    binding.smartRefresh.finishLoadMore()
                    if (page == 1) {
                        adapter.setNewData(it)
                    } else {
                        adapter.addData(it)
                    }
                    page++
                } else {
                    binding.smartRefresh.finishLoadMore()
                }
            }

        })

    }

    private fun initListener() {
        binding.ivBack.setOnClickListener { finish() }
        adapter.setOnItemClickListener { adapter, view, position ->
            if (!DoubleUtils.isFastDoubleClick()) {
                //百科详情
                val wikiBean = adapter.data[position] as WikiHomeAmusingMoreRespWiki.DataBean
                val map = HashMap<String, Any>()
                map["wiki_amusing_id"] = wikiBean.id
                ARouterUtils.jump(ActivityRouter.COMMUNITY.WIKI_DETAIL, map)
            }
        }

        binding.smartRefresh.setOnRefreshListener {
            page = 1
            loadData()
        }
        binding.smartRefresh.setOnLoadMoreListener {
            if (hasnext) {
                loadData()
            } else {
                binding.smartRefresh.finishLoadMore()
            }
        }
    }

    fun loadData() {
        mViewModel.reqWikiList(page)
    }
}